function $key(getkeys) {
    return document.querySelector(getkeys);
}
var top = document.getElementsByClassName("top")[0];
var nav = document.getElementsByClassName("nav")[0];
// 提取日期相关函数
date();
function date() {
    var spa = document.querySelector(".dater");
    var timer = null;
    // 封装时间日期格式化
    function mydate() {
        var date = new Date();
        // 时间日期格式化 yyyy-mm-dd hh:mm:ss
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var data = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        // 补 0
        month = (month < 10) ? "0" + month : month;
        data = (data < 10) ? "0" + data : data;
        hours = (hours < 10) ? "0" + hours : hours;
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;
        var res = `${year}年${month}月${data}日 ${hours}:${minutes}:${seconds}`;
        return res;
    }

    function automove() {
        spa.innerHTML = mydate();
    }
    // 自动走
    setInterval(automove, 1000);
}
var username = sessionStorage.getItem('userAccount');
document.querySelector('.namer').textContent = username;

// 班级详情列表
var Tbody = document.querySelector(".Tbody");
var pageIndex = 1;
var pageSize = 5;
var classid = JSON.parse(localStorage.getItem("classId"));
console.log(classid);
function mov() {
    var data = {
        clazzId: classid,
        pageIndex: pageIndex,
        pageSize: pageSize,
    }
    ajax.post("http://192.168.199.101:8080/BaorSystem/clazz/clazzStudents", data, function (res) {
        if (res.code == 200) {
            console.log(res);
            clazzList = res.dataset.clazzList;
            for (var item of clazzList) {
                var newTr = document.createElement("tr");
                newTr.innerHTML = `
                     <td>${item.fileManagerName}</td>
                    <td>${item.fileManagerSex}</td>
                    <td>${item.fileManagerSosphone}</td>
                    <td>${item.fileManagerAddress}</td>
                    <td>${item.fileManagerSosperson}</td>
                    <td>${item.fileManagerSosphone}</td>
                    <td><button class="revis">修改</button><button class="detai">删除</button></td>
                `
                Tbody.appendChild(newTr);

            }
            revise();
            detail();
            pageCount = res.dataset.pageCount;
            var fot = document.querySelector(".fot");
            fot.innerHTML = "";
            for (var i = 1; i <= pageCount; i++) {
                var But = document.createElement("button");
                But.innerText = i;
                But.className = "page";
                fot.appendChild(But);
            }
        }
    })
}
mov();

//修改
function revise() {
    var revis = document.getElementsByClassName("revis");
    for (var i = 0; i < revis.length; i++) {
        revis[i].onclick = function () {
            for (var j = 0; j < revis.length; j++) {
                if (this == revis[j]) {
                    fileManagerId = clazzList[j].fileManagerId;
                    console.log(fileManagerId);
                    var Masks = document.createElement("div");
                    Masks.className = "Masks";
                    $key(".nav_content").insertBefore(Masks, $key(".nav_content").firstElementChild);
                    var Modal = document.createElement("div");
                    Modal.className = "Modal";
                    Modal.innerHTML = `
                            <div class="wtp">修改学生信息</div> 
                            <div class="wto">
                                <div>姓名：<input class="cs1" type="text"></div>
                                <div>家庭住址：<input  class="cs2" type="text"></div>
                                <div>性别：<select class="charg">
                                    <option>男</option>
                                     <option>女</option>
                                </select></div>
                                <div>班级ID：<select class="cla"></select></div>
                                <div>紧急联系人：<input class="cs3" type="text"></div>
                                 <div>电话：<input class="cs4" type="text"></div>
                                  <div>紧急联系人电话：<input class="cs5" type="text"></div>  
                            </div>
                            <div class="wtf">
                                <button class="Confirr">确认</button>
                                <button class="Cance">取消</button>
                            </div>
                            `
                    $key(".nav_content").insertBefore(Modal, $key(".nav_content").firstElementChild.nextSibling);
                    // charge();'
                    //班级id
                    var cla = document.getElementsByClassName("cla")[0];
                    ajax.post("http://192.168.199.101:8080/BaorSystem/fileManager/clazz", {}, function (trw) {
                        if (trw.code == 200) {
                            var dataset = trw.dataset;
                            console.log(dataset);
                            for (var i = 0; i < dataset.length; i++) {
                                var newoption = document.createElement("option");
                                newoption.innerHTML = `${dataset[i].clazzName}`;
                                newoption.value = `${dataset[i].clazzId}`;
                                cla.appendChild(newoption);
                            }

                        }
                    })
                    Cancel();
                    //确认
                    fonfg();
                }
            }
        }
    }
}

//确认
function fonfg() {
    var Masks = document.querySelector(".Masks");
    var Modal = document.querySelector(".Modal");
    var Confirr = document.getElementsByClassName("Confirr")[0];
    Confirr.onclick = function () {
        var cs1 = document.getElementsByClassName("cs1")[0];
        var cs2 = document.getElementsByClassName("cs2")[0];
        var cs3 = document.getElementsByClassName("cs3")[0];
        var cs4 = document.getElementsByClassName("cs4")[0];
        var cs5 = document.getElementsByClassName("cs5")[0];
        var charg = document.querySelector(".charg");
        var cla = document.getElementsByClassName("cla")[0];
        var slt = {
            fileManagerId: fileManagerId,
            fileManagerName: cs1.value,
            fileManagerAddress: cs2.value,
            fileManagerSex: charg.value,
            fileManagerSosperson: cs3.value,
            fileManagerPhone: cs4.value,
            fileManagerSosphone: cs5.value,
            clazzId: cla.value,
        }
        ajax.post("http://192.168.199.101:8080/BaorSystem/fileManager/updateFileManager", slt, function (sur) {
            if (sur.code == 200) {
                console.log(sur);
            }
        })
        $key(".nav_content").removeChild(Masks);
        $key(".nav_content").removeChild(Modal);
    }
}

// 取消
function Cancel() {
    var Cance = document.getElementsByClassName("Cance")[0];
    var Masks = document.querySelector(".Masks");
    var Modal = document.querySelector(".Modal");
    Cance.addEventListener("click", function () {
        $key(".nav_content").removeChild(Masks);
        $key(".nav_content").removeChild(Modal);
    })
}

//删除
function detail() {
    var detai = document.querySelectorAll(".detai");
    for (var i = 0; i < detai.length; i++) {
        detai[i].onclick = function () {
            for (var j = 0; j < detai.length; j++) {
                if (this == detai[j]) {
                    var fileManagerId = clazzList[j].fileManagerId;
                    console.log(fileManagerId);
                    var ufo = {
                        fileManagerId: fileManagerId,
                        fileManagerType: "在习生",
                    }
                    ajax.post("http://192.168.199.101:8080/BaorSystem/fileManager/deleteFileManager", ufo, function (woo) {
                        if (woo.code == 200) {
                            // mov();
                        }
                    })
                }
            }
        }
    }
}


//页面跳转
//班级管理
var classs = document.getElementsByClassName("classs");
classs[0].onclick = function () {
    location.href = "./ClassManagement.html";
}
//日常
var routine = document.getElementsByClassName("routine")[0];
routine.onclick = function () {
    location.href = "./daily.html";
}
//项目管理
var project = document.getElementsByClassName("project")[0];
project.onclick = function () {
    location.href = "./projectmanagement.html";
}
//出差记录
var travel = document.querySelector(".travel");
travel.addEventListener('click', function () {
    location.href = "./business.html";
});
//请假记录
var excused = document.querySelector(".excused");
excused.addEventListener('click', function () {
    location.href = "./vacate.html";
});
//考勤记录
var presence = document.querySelector(".presence");
presence.addEventListener('click', function () {
    location.href = "./clock.html";
});
//系统设置
var system = document.getElementsByClassName("system")[0];
system.onclick = function () {
    location.href = "./setting.html";
}